﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SMS.ServiceManager;
using digit.Core;
using SMS.UserManager;

namespace SMS.Admin.Modules
{
    public partial class ListMessageError : WebControlBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!ClientState.IsAuthenticated)
            {
                Response.Redirect("~/Login.aspx?session=timeout");
            }

            this.lnkDel.Attributes.Add("onclick", "return confirm('Bạn đã chắc chắn xóa?');");
            this.lnkDelBot.Attributes.Add("onclick", "return confirm('Bạn đã chắc chắn xóa?');");

            if (IsPostBack) return;

            if (ClientState.MemberInfo.UserType == 0 || ClientState.MemberInfo.UserType == 1 || ClientState.MemberInfo.UserType == 5)
            {
                this.BindData();
            }
            else
                Response.Redirect("default.aspx?module=Warning");
        }

        protected void lnkDel_Click(object sender, EventArgs e)
        {
            try
            {
                int iDeleted = 0;

                //Search all checked rows to delete
                foreach (GridViewRow r in this.grvSms.Rows)
                {
                    //If checkbox is not checked then quit
                    HtmlInputCheckBox cbx = (HtmlInputCheckBox)r.Cells[0].Controls[1];
                    if (!cbx.Checked) continue;

                    Label lblMobile = (Label)r.FindControl("lblMobileNo");
                    Label lblServiceID = (Label)r.FindControl("lblServiceID");

                    string strUserName = "";
                    if (ClientState.MemberInfo.Username == "") continue;
                    else
                        strUserName = ClientState.MemberInfo.Username;

                    //If checkbox is checked then delete
                    MessageInManager.DeleteError(long.Parse(cbx.Value), lblMobile.Text.Trim(), lblServiceID.Text.Trim(), strUserName);

                    iDeleted++;
                }

                //Display result of delete process
                if (iDeleted == 0)
                {
                    this.lblError.Text = "Không có bản ghi nào được cập nhật!";
                    return;
                }

                this.lblError.Text = "Đã xóa " + iDeleted.ToString() + " bản ghi!";
                BindData();
            }
            catch { lblError.Text = "Cập nhật lỗi!"; }
        }

        protected void lnkChoose_Click(object sender, EventArgs e)
        {
            try
            {
                int iUpdated = 0;

                //Search all checked rows to update
                foreach (GridViewRow r in this.grvSms.Rows)
                {
                    //If checkbox is not checked then quit
                    HtmlInputCheckBox cbx = (HtmlInputCheckBox)r.Cells[0].Controls[1];
                    if (!cbx.Checked) continue;

                    Label lblMobile = (Label)r.FindControl("lblMobileNo");
                    Label lblServiceID = (Label)r.FindControl("lblServiceID");

                    string strUserName = "";
                    if (ClientState.MemberInfo.Username == "") continue;
                    else
                        strUserName = ClientState.MemberInfo.Username;

                    //If checkbox is checked then update
                    MessageInManager.MoveError(long.Parse(cbx.Value), lblMobile.Text.Trim(), lblServiceID.Text.Trim(), strUserName);

                    iUpdated++;
                }

                //Display result of update process
                if (iUpdated == 0)
                {
                    this.lblError.Text = "Không có bản ghi nào được cập nhật!";
                    return;
                }

                this.lblError.Text = "Đã cập nhật " + iUpdated.ToString() + " bản ghi!";
                BindData();
            }
            catch { lblError.Text = "Cập nhật lỗi!"; }
        }

        protected void grvSms_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.grvSms.PageIndex = e.NewPageIndex;
            this.BindData();
        }

        protected void grvSms_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (ViewState["SortExpression"].ToString() == e.SortExpression)
            {
                ViewState["SortDirection"] = ((SortDirection)ViewState["SortDirection"] == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending);
            }
            else
            {
                ViewState["SortExpression"] = e.SortExpression;
                ViewState["SortDirection"] = SortDirection.Ascending;
            }
            BindData();
        }

        private void BindData()
        {
            try
            {
                DataTable dt = MessageInManager.SelectAll_MessageError();

                if (dt.Rows.Count > 0)
                {
                    this.grvSms.DataSource = dt;
                    this.grvSms.DataBind();
                    this.lblError.Text = "Tổng số <b>" + dt.Rows.Count + "</b> tin phát sinh lỗi";
                }
                else
                {
                    this.grvSms.DataSource = null;
                    this.grvSms.DataBind();
                    lblError.Text = "Hiện tại chưa có tin lỗi nào";
                }
            }
            catch { }
        }
    }
}
